package com.mechat.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.mechat.BuildConfig
import com.mechat.Constant
import io.rong.imkit.SpName
import com.mechat.entity.LoginParamEntity
import com.google.gson.JsonObject
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.manager.SDActivityManager
import com.custom.base.mvp.BasePresenterImpl
import com.custom.base.util.IntentUtil
import com.mechat.ui.HomeActivity


/**
 * 作者：
 * 时间：
 * 描述：登录
 */
class LoginPresenter : BasePresenterImpl<LoginContract.View>(), LoginContract.Presenter {
    override fun initBundle(bundle: Bundle) {
        mView?.apply {
            editAccount.addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    txtLogin.isEnabled= editAccount.text.toString().isNotEmpty() && editPwd.text.toString().isNotEmpty()
                }

                override fun afterTextChanged(p0: Editable?) {

                }

            })
            editPwd.addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    txtLogin.isEnabled= editAccount.text.toString().isNotEmpty() && editPwd.text.toString().isNotEmpty()
                }

                override fun afterTextChanged(p0: Editable?) {

                }

            })
            txtLogin.setOnClickListener {
                OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                    override fun addBody(requestBody: OkHttpBodyEntity) {
                        requestBody.setPost(Constant.login_url)
                        requestBody.add("account",editAccount.text.toString())
                        requestBody.add("accountPassword",editPwd.text.toString())
                        requestBody.add("loginType",4)
                    }
                }, object : SDOkHttpResoutCallBack<LoginParamEntity>() {
                    override fun onSuccess(entity: LoginParamEntity) {
                        val jsonObject = JsonObject()
                        jsonObject.addProperty("channel","l1")
                        jsonObject.addProperty("appCode","sparksy")
                        jsonObject.addProperty("appVersion", BuildConfig.VERSION_CODE)
                        jsonObject.addProperty("token",entity.data.token)
                        BaseConfig.getInstance.addHead("basicParams", jsonObject.toString())
                        BaseConfig.getInstance.setString(SpName.token,entity.data.token)
                        BaseConfig.getInstance.setString(SpName.userCode,entity.data.userCode)
                        IntentUtil.startActivity(HomeActivity::class.java)
                        SDActivityManager.instance.finishAllActivityExcept(HomeActivity::class.java)
                    }

                    override fun onFailure(code: Int, msg: String) {
                        txtErrorTip.text="*$msg"
                    }
                })
            }
        }
    }
}